package cn.jja8.bukkit.bukkitcommandtool.command;

import org.bukkit.command.CommandSender;

import java.util.Collections;
import java.util.List;

public class Command implements CommandImplement,CanSetUp{
    /**名称，也作为主命令*/
    private final String name;
    /**别称*/
    private List<String> aliases;
    /**权限*/
    String power;
    /**语言*/
    CommandLanguage language;
    /**命令执行器*/
    CommandImplement commandImplement = new CommandImplementNoSet();
    Command(CommandLanguage language,String name) {
        this.language = language;
        this.name = name;
    }

    public Command(String name) {
        this(new CommandLanguage(),name);
    }

    CommandLanguage getLanguage(){
        return language;
    }

    //api-------------------------------------------------------------------
    /**
     * 设置命令执行器
     * */
    public Command setCommandImplement(CommandImplement commandImplement) {
        if (commandImplement==null){
            commandImplement = new CommandImplementNoSet();
        }
        this.commandImplement = commandImplement;
        return this;
    }

    /**
     * 设置别称
     * */
    public Command setAliases(List<String> aliases) {
        this.aliases = aliases;
        return this;
    }
    /**
     * 设置权限
     * */
    public Command setPower(String power) {
        this.power = power;
        return this;
    }



    //实现接口--------------------------------------------------------
    @Override
    public boolean command(CommandSender commandSender, String[] args) {
        if (power==null||commandSender.hasPermission(power)) {
            return commandImplement.command(commandSender,args);
        }else {
            commandSender.sendMessage(getLanguage().NoPower);
            return true;
        }
    }

    @Override
    public List<String> tabCompletion(CommandSender commandSender, String[] strings) {
        if (power==null||commandSender.hasPermission(power)){
            return commandImplement.tabCompletion(commandSender,strings);
        }else {
            return Collections.emptyList();
        }
    }

    @Override
    public void load(ConfigurationSectionSetDefGet configurationSectionSetDefGet) {
        aliases = configurationSectionSetDefGet.getStringListSetDef("aliases",aliases);
        power = configurationSectionSetDefGet.getStringSetDef("power",power);
        language = configurationSectionSetDefGet.getObjectSetDef("language", language);
        if (commandImplement instanceof CanSetUp){
            commandImplement = (CommandImplement) configurationSectionSetDefGet.getObjectSetDef("commandImplement",(CanSetUp)commandImplement);
        }
    }

    //非api---------------------------
    /**
     * 获取主命令
     * */
    String getMainCommand() {
        return name;
    }

    /**
     * 获取别称
     * */
    List<String> getAliases() {
        return aliases;
    }
}
